using System;
using System.Collections.Generic;
using System.Text;
using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;

namespace replaceconstwithvar
{
    class replaceconstwithvar
    {
        static void Main(string[] args)
        {
            // create a sql parser instance supports ms sql server sql dialect
            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);

            //sqlparser.SqlText.Text = @"select salaryid from salarycode where controlcodes like '%Diet%' or (regtypeid = 1 and icon = 'car') or salarycoderef in('so', 'io')";
            // sqlparser.SqlText.Text =  @"UPDATE employee SET employeename='arun', lastname='deep' WHERE employeeid =1";
            sqlparser.SqlText.Text = @"INSERT INTO employee(employeename,lastname) VALUES ('arun','deep')";
            int iRet = sqlparser.Parse();
            int counter = 0;
            if (iRet == 0)
            {
                foreach (TCustomSqlStatement sql in sqlparser.SqlStatements)
                {
                    foreach (TSourceToken st in sql.SourceTokenList)
                    {
                        if 
                            (
                            (st.TokenType == TTokenType.ttNumber)
                            || (st.TokenType == TTokenType.ttSQString)
                            )
                        {
                            counter++;
                            st.AsText = "@var" + counter.ToString();

                        }
                    }

                    Console.WriteLine("{0}", sql.AsText);
                }
            }
            else
            {
                Console.WriteLine("Syntax error found in input sql:");
                Console.WriteLine(sqlparser.ErrorMessages);
            }

        }
    }
}
